<?php
namespace App\Models;


/**
 * 收货地址类
 */
class MAddress extends BaseModel
{
    public $table = "address";

    /**
     * 收货地址数据验证规则
     * $flag 0 = rules  1 = msg
     */
    public static function saveRulesWithMsg($flag)
    {
        $rules = array(
            "name"   => "required|alpha|between:2,50",
            'tel'    => "required|between:7,20",
            'province'   => 'required|max:255',
            'city'   => 'required|max:255',
            'county'   => 'required|max:255',
            'addr' => 'required|max:255',
        );

        $msg = array(
            "name.required"   => "请填写收货人姓名！",
            "tel.required"    => "请填写您的联系方式！",
            "province.required"   => "请选择省！",
            "city.required"   => "请选择市！",
            "county.required"   => "请选择区！",
            "addr.required" => "请填写您的街道地址！"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }


    /**
     * 存储收货地址
     */
    public static function saveAddress($request, $uid)
    {

        $address = MAddress::where('uid', $uid)->first();
        if (empty($address)) {
            $address = new MAddress();
        }
        $address->uid = $uid;
        $address->name = $request->name;
        $address->tel = $request->tel;
        $address->province = $request->province;
        $address->city = $request->city;
        $address->county = $request->county;
        $address->addr = $request->addr;
        $address->time = time();

        return $address->save();
    }

}